package controllers;

import connectivity.Dbmanager;
import connectivity.QueryManager;
import java.io.*;
import java.util.LinkedList;
import javax.servlet.*;
import javax.servlet.http.*;
import models.User;
import models.Course;
import models.Module;

public class ModuleListController extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Dbmanager dbManager = new Dbmanager();
        dbManager.openConnection();
        QueryManager queryManager = new QueryManager(dbManager);
        int courseID;
        Course course;
        LinkedList<Module> modules = new LinkedList<Module>();

        if (request.getParameter("tryoutid") != null) {
            courseID = Integer.parseInt(request.getParameter("tryoutid"));
        } else {
            courseID = Integer.parseInt(request.getParameter("id"));
        }

        modules = queryManager.getAllModules(courseID);

        if (request.getParameter("tryoutid") != null) {
            Module tryoutmodule = null;
            try {
                tryoutmodule = modules.get(0);
            } catch (Exception e) {
            }
            modules = new LinkedList<Module>();
            modules.add(tryoutmodule);
        }

        course = queryManager.getCoursbyid(courseID);
        request.setAttribute("course", course);
        request.setAttribute("modulesUitSessie", modules);
        request.setAttribute("courseID", courseID);
        dbManager.closeConnection();

        RequestDispatcher dispatcher = request.getRequestDispatcher("/moduleList.jsp");
        dispatcher.forward(request, response);

    }
}
